/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: fat file system
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2022-08-15     JasonHu           Port from xboot
 */

#ifndef __FAT_NODE_H__
#define __FAT_NODE_H__

#include "fat.h"

#include <nxos.h>

/*
 * Information for accessing a FAT file/directory
 */
struct fatfs_node_t {
	/* Parent FAT control */
	struct fatfs_control_t * ctrl;

	/* Parent directory entry */
	struct fatfs_node_t * parent;
	NX_U32 parent_dent_off;
	NX_U32 parent_dent_len;
	struct fat_dirent_t parent_dent;
	NX_Bool parent_dent_dirty;

	/* First cluster */
	NX_U32 first_cluster;

	/* Current cluster */
	NX_U32 cur_cluster;
	NX_U32 cur_pos;

	/* Cached clusters */
	NX_U8 *cached_data;
	NX_U32 cached_clust;
	NX_Bool cached_dirty;
};

NX_U32 fatfs_node_get_size(struct fatfs_node_t * node);
NX_U32 fatfs_node_read(struct fatfs_node_t * node, NX_U32 pos, NX_U32 len, NX_U8 * buf);
NX_U32 fatfs_node_write(struct fatfs_node_t * node, NX_U32 pos, NX_U32 len, NX_U8 * buf);
int fatfs_node_truncate(struct fatfs_node_t * node, NX_U32 pos);
int fatfs_node_sync(struct fatfs_node_t * node);
int fatfs_node_init(struct fatfs_control_t * ctrl, struct fatfs_node_t * node);
int fatfs_node_exit(struct fatfs_node_t * node);
int fatfs_node_read_dirent(struct fatfs_node_t * dnode, NX_I64 off, NX_Dirent * d);
int fatfs_node_find_dirent(struct fatfs_node_t * dnode, const char * name, struct fat_dirent_t * dent, NX_U32 * dent_off, NX_U32 * dent_len);
int fatfs_node_add_dirent(struct fatfs_node_t * dnode, const char * name, struct fat_dirent_t * ndent);
int fatfs_node_del_dirent(struct fatfs_node_t * dnode, const char * name, NX_U32 dent_off, NX_U32 dent_len);

#endif /* __FAT_NODE_H__ */
